package com.oscar.ehome.view.teamview.adapter;

import android.content.Context;
import android.graphics.Color;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.oscar.ehome.R;
import com.oscar.ehome.base.C;
import com.oscar.ehome.bean.teamBean.UserAppInfoBean;
import com.oscar.ehome.provider.gildeimage.GildeUtils;
import com.oscar.ehome.utils.basethem.TintManager;
import com.oscar.ehome.utils.system.SystemConfigFactory;
import com.oscar.ehome.utils.view.MyImageView;
import com.oscar.ehome.view.teamview.TeamDetailActivity;

import java.util.List;


public class GridviewDetialAdapter extends BaseAdapter {
    private LayoutInflater mInflater;
    private Context context;
    private List<UserAppInfoBean.ChildrenBean> children;
    UserDetialIntentListener intentListener;

    public GridviewDetialAdapter(TeamDetailActivity context, List<UserAppInfoBean.ChildrenBean> children) {
        this.context = context;
        this.children = children;
    }

    public void setList(List<UserAppInfoBean.ChildrenBean> children) {
        this.children = children;
        notifyDataSetChanged();
    }

    public interface UserDetialIntentListener {
        public void onIntentClick(int postion);
    }

    public void setOnIntentClickListener(UserDetialIntentListener intentListener) {
        this.intentListener = intentListener;
    }

    @Override
    public int getCount() {

        if (children.size() < 5) {
            return children.size();
        }
        return 5;

    }

    @Override
    public Object getItem(int position) {
        return children.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        if (convertView == null) {
            viewHolder = new ViewHolder();
            convertView = View.inflate(parent.getContext(), R.layout.item_grid, null);
            viewHolder.title = (TextView) convertView.findViewById(R.id.tv_detial_gr);
            viewHolder.image = (MyImageView) convertView.findViewById(R.id.im_detial_grss);
            viewHolder.im_gengduo = (ImageView) convertView.findViewById(R.id.im_gengduo);
            viewHolder.rrll_a = (LinearLayout) convertView.findViewById(R.id.rrll_a);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        viewHolder.rrll_a.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                intentListener.onIntentClick(position);
            }
        });
        viewHolder.im_gengduo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                intentListener.onIntentClick(position);
            }
        });

        UserAppInfoBean.ChildrenBean bean = children.get(position);
        if (bean != null && !("").equals(bean)) {
            if (position == 4) {
                viewHolder.rrll_a.setVisibility(View.GONE);
                viewHolder.im_gengduo.setVisibility(View.VISIBLE);
                viewHolder.im_gengduo.setImageResource(R.mipmap.gengduotx);
                TintManager.setTint(viewHolder.im_gengduo, Color.parseColor(SystemConfigFactory.getInstance(context).getSystemConfig().getColor()));
            } else {
                viewHolder.title.setText(bean.getName());
//                GildeUtils.imgLoadPic(context, C.qiniu + bean.getPhoto(), viewHolder.image, "net-p");
                GildeUtils.loadUrlImage4(C.qiniu + bean.getPhoto(), viewHolder.image, context);
                viewHolder.rrll_a.setVisibility(View.VISIBLE);
                viewHolder.im_gengduo.setVisibility(View.GONE);
            }
            Log.d("mwh", "adapter");
        }

        return convertView;
    }


    static class ViewHolder {
        TextView title;
        MyImageView image;
        ImageView im_gengduo;
        LinearLayout rrll_a;
    }

}
